package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.PagesCategories;

public class GenericPagesCatDAO extends AutoKeys implements
		org.jetnuke.dao.PagesCatDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericPagesCatDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PagesCatModel#retrieveByPK(int)
	 */
	public PagesCategories retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PageCatModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		PagesCategories s = new PagesCategories();

		if (rs.next()) {
			fillPageCatFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillPageCatFromResultSet(PagesCategories s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPageCatFromResultSet(PagesCategories, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setCID(rs.getInt("CID"));
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setTITLE(rs.getString("TITLE"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPageCatFromResultSet(PagesCategories, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			PagesCategories u = new PagesCategories();
			fillPageCatFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PageCatModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PagesCatModel#addNew(org.jetnuke.entities.PagesCategories)
	 */
	public int addNew(PagesCategories cat) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(PagesCategories) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("PageCatModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("PagesCatModel.lastGeneratedPagesCatID"));
		initNewPagesCat(cat, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(PagesCategories) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param cat
	 * @param p
	 */
	private void initNewPagesCat(PagesCategories cat, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewPagesCat(PagesCategories, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, cat.getDESCRIPTION());
		p.setString(2, cat.getTITLE());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewPagesCat(PagesCategories, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PagesCatModel#update(org.jetnuke.entities.PagesCategories)
	 */
	public void update(PagesCategories cat) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(PagesCategories) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("PageCatModel.update");
		p.setString(1, cat.getDESCRIPTION());
		p.setString(2, cat.getTITLE());
		p.setInt(3, cat.getCID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(PagesCategories) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PagesCatModel#selectAllOrderCID()
	 */
	public List selectAllOrderCID() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderCID() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PageCatModel.selectAllOrderCID"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderCID() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PagesCatModel#selectAllDifferentCID(int)
	 */
	public List selectAllDifferentCID(int cid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllDifferentCID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("PageCatModel.selectAllDifferentCID"));
		p.setInt(1, cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllDifferentCID(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

}
